using RaceHFTools.BluetoothLE.Bean;
using System;
using System.Threading.Tasks;

using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace RaceHFTools.Pages.Tweak
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class TweakUserPage : ContentPage
    {
        private readonly Bean bean;

        public TweakUserPage(Bean bean)
        {
            InitializeComponent();

            this.bean = bean;
        }

        #region 事件处理
        bool nick_set_done, id_set_done;

        void OnAllSetDone()
        {
            if (nick_set_done && id_set_done)
            {
                Device.BeginInvokeOnMainThread(async () => await DisplayAlert("用户信息设置", "设置完毕", "确定"));
            }
        }

        void ResetAllFlags()
        {
            nick_set_done = false;
            id_set_done = false;
        }

        void OnNickGet(string nick)
        {
            Device.BeginInvokeOnMainThread(() =>
            {
                if (nick != "\0")
                    etyUserNick.Text = nick;
            });
        }

        void OnIdGet(uint id)
        {
            Device.BeginInvokeOnMainThread(() =>
            {
                etyUserId.Text = id.ToString();
            });
        }

        void OnNickSet(bool success)
        {
            nick_set_done = success;
            OnAllSetDone();
        }

        void OnIdSet(bool success)
        {
            id_set_done = success;
            OnAllSetDone();
        }

        async Task<bool> DoNickSet()
        {
            string nick = etyUserNick.Text?.Trim();

            if (nick?.Length >= 25)
            {
                Device.BeginInvokeOnMainThread(async () => await DisplayAlert("用户昵称", "昵称长度超过25字符", "确定"));
                return false;
            }

            await bean.Tweak.SetUserNick(nick);

            return true;
        }

        async Task<bool> DoIdSet()
        {
            if (!uint.TryParse(etyUserId.Text?.Trim(), out uint id))
            {
                Device.BeginInvokeOnMainThread(async () => await DisplayAlert("用户Id", "Id错误", "确定"));
                return false;
            }

            await bean.Tweak.SetUserId(id);

            return true;
        }
        #endregion

        #region OnAppearing/OnDisappearing
        protected override void OnAppearing()
        {
            base.OnAppearing();

            bean.Tweak.UserGetNickEvent += OnNickGet;
            bean.Tweak.UserGetIdEvent += OnIdGet;
            bean.Tweak.UserSetNickEvent += OnNickSet;
            bean.Tweak.UserSetIdEvent += OnIdSet;

            _ = bean.Tweak.GetUserAll();
        }

        protected override void OnDisappearing()
        {
            bean.Tweak.UserGetNickEvent -= OnNickGet;
            bean.Tweak.UserGetIdEvent -= OnIdGet;
            bean.Tweak.UserSetNickEvent -= OnNickSet;
            bean.Tweak.UserSetIdEvent -= OnIdSet;

            base.OnDisappearing();
        }
        #endregion

        #region 交互事件
        async void Button_UserApply_Clicked(object sender, EventArgs e)
        {
            ResetAllFlags();

            await DoNickSet();
            await DoIdSet();
        }
        #endregion
    }
}
